package com.example.score_reception.config;

import com.example.score_reception.service.Impl.StudentServiceImpl;
import com.example.score_reception.service.Impl.TeacherServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
/**
 * @author shenrui
 * @date 2021/9/30
 * @description 继承WebSecurityConfigurerAdapter，创建了student和teacher两条过滤器链
 */
@Configuration
//激活方法上面的注解
@EnableGlobalMethodSecurity(prePostEnabled = true,securedEnabled =true)
public class SecurityConfiguration {
    @Order(1)
    @Configuration
    public static class StudentSecurityConfiguration extends WebSecurityConfigurerAdapter {

        @Autowired
        private StudentServiceImpl studentService;
        @Override
        protected void configure(HttpSecurity http) throws Exception {
            // 一般来说security自动开启防御csrf，类似那种ajax post请求就访问不了，可以调用disable方法注销调，当然这不安全。
            http
                    .csrf().disable()
                    .authorizeRequests()
                    // http.permitAll不会绕开springsecurity验证，相当于是允许该路径通过

                    .and()
                    .antMatcher( "/student/**" )
                    .authorizeRequests().antMatchers( "/student/**" ).authenticated()
                    .antMatchers("/").permitAll()
                    .anyRequest().authenticated()
                    .and()
                    .formLogin()
                    // 配置登录页面的访问路径
                    .loginPage("/student/student_login")
                    .loginProcessingUrl("/student/do_student_login")
                    .usernameParameter("stuNumber")//设置登录请求接口的参数（用户名）
                    .passwordParameter("stuPassword")//设置登录请求接口的参数（密码）
                    .defaultSuccessUrl("/student/student_main",true)
                    // 登录路径允许不用验证即可访问
                    .permitAll()
                    .and()
                    // Spring Security已经有登出的方法，这里配置登出也是可以直接访问
                    .logout()
                    .logoutUrl("/student/logout")
                    .logoutSuccessUrl("/student/student_login")//注销后跳转到哪一个页面
                    .permitAll()
                    .and()
                    //只允许登录一个
                    .sessionManagement()
                    .maximumSessions(1).expiredUrl("/student/student_login");
        }



        //使用BCryptPasswordEncoder进行加密
        @Override
        protected void configure(AuthenticationManagerBuilder auth) throws Exception {
            auth.userDetailsService(studentService).passwordEncoder(new BCryptPasswordEncoder());
        }

    }
    @Configuration
    public static class TeacherSecurityConfiguration extends WebSecurityConfigurerAdapter {

        @Autowired
        private TeacherServiceImpl teacherService;
        @Override
        protected void configure(HttpSecurity http) throws Exception {
            // 一般来说security自动开启防御csrf，类似那种ajax post请求就访问不了，可以调用disable方法注销调，当然这不安全。
            http
                    .csrf().disable()
                    .authorizeRequests()
                    // http.permitAll不会绕开springsecurity验证，相当于是允许该路径通过

                    .and()
                    .antMatcher( "/teacher/**" )
                    .authorizeRequests().antMatchers( "/teacher/**" ).authenticated()
                    .antMatchers("/").permitAll()
                    .anyRequest().authenticated()
                    .and()
                    .formLogin()
                    // 配置登录页面的访问路径
                    .loginPage("/teacher/teacher_login")
                    .loginProcessingUrl("/teacher/do_teacher_login")
                    .usernameParameter("teacherNumber")//设置登录请求接口的参数（用户名）
                    .passwordParameter("teacherPassword")//设置登录请求接口的参数（密码）
                    .defaultSuccessUrl("/teacher/teacher_main",true)
                    // 登录路径允许不用验证即可访问
                    .permitAll()
                    .and()
                    // Spring Security已经有登出的方法，这里配置登出也是可以直接访问
                    .logout()
                    .logoutUrl("/teacher/logout")
                    .logoutSuccessUrl("/teacher/teacher_login")//注销后跳转到哪一个页面
                    .permitAll()
                    .and()
                    //只允许登录一个
                    .sessionManagement()
                    .maximumSessions(1).expiredUrl("/teacher/teacher_login");
        }



        //使用BCryptPasswordEncoder进行加密
        @Override
        protected void configure(AuthenticationManagerBuilder auth) throws Exception {
            auth.userDetailsService(teacherService).passwordEncoder(new BCryptPasswordEncoder());
        }

    }
}
